package com.las.nogu.controller.vits;

import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import cn.hutool.http.HttpUtil;
import com.alibaba.fastjson.JSONObject;
import com.las.bot.dao.VitsModelDao;
import com.las.bot.dao.VitsUserDao;
import com.las.nogu.dto.VitsDTO;
import com.model.VitsModel;
import com.model.VitsUser;
import com.utils.AiUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/vits")
public class VitsController {

    @Autowired
    private VitsUserDao vitsUserDao;

    @Autowired
    private VitsModelDao modelDao;


    @RequestMapping("/getAI/{uId}/{token}/{mId}/{rId}/{text}")
    public String auth(@PathVariable("uId") String uId, @PathVariable("token") String token,
                       @PathVariable("mId") String mId, @PathVariable("rId") String rId,
                       @PathVariable("text") String text) throws Exception {
        VitsModel vitsModel = modelDao.findById(mId);
        if (ObjectUtil.isNull(vitsModel)) {
            return "NONE";
        }
        VitsUser vitsUser = vitsUserDao.findByUserAndCode(uId, mId + "-" + rId);
        if (ObjectUtil.isNull(vitsUser)) {
            return "NONE";
        }
        String uuid = vitsUser.getToken();
        if(!uuid.equals(token)){
            return "NONE";
        }
        if(StrUtil.isBlank(text)){
            return "NONE";
        }
        if(text.length() > 50){
            return "NONE";
        }
        String fId = AiUtil.getAvUrl(text, vitsModel.getModelPath(), vitsModel.getConfigPath(), rId);
        if(StrUtil.isBlank(fId)){
            return "NONE";
        }
        String result = HttpUtil.post("http://vits.natapp1.cc/sovits/getBase64ById?fId=" + fId, (String) null);
        JSONObject object = JSONObject.parseObject(result);
        return object.getString("data");
    }


    @RequestMapping("/getAI2")
    public String auth2(@RequestBody VitsDTO vitsDTO) throws Exception {
        VitsModel vitsModel = modelDao.findById(vitsDTO.getmId());
        if (ObjectUtil.isNull(vitsModel)) {
            return "NONE";
        }
        VitsUser vitsUser = vitsUserDao.findByUserAndCode(vitsDTO.getuId(), vitsDTO.getmId() + "-" + vitsDTO.getrId());
        if (ObjectUtil.isNull(vitsUser)) {
            return "NONE";
        }
        String uuid = vitsUser.getToken();
        if(!uuid.equals(vitsDTO.getToken())){
            return "NONE";
        }
        if(StrUtil.isBlank(vitsDTO.getText())){
            return "NONE";
        }
        if(vitsDTO.getText().length() > 50){
            return "NONE";
        }
        String fId = AiUtil.getAvUrl(vitsDTO.getText(), vitsModel.getModelPath(), vitsModel.getConfigPath(), vitsDTO.getrId());
        if(StrUtil.isBlank(fId)){
            return "NONE";
        }
        String result = HttpUtil.post("http://vits.natapp1.cc/sovits/getBase64ById?fId=" + fId, (String) null);
        JSONObject object = JSONObject.parseObject(result);
        return object.getString("data");
    }


}
